package com.sxm.yiwei.activitis;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.FWContentImgAdapter;
import com.sxm.yiwei.tools.CallPhone;
import com.sxm.yiwei.tools.CastTime;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.tools.ImgSwitcherActivity;
import com.sxm.yiwei.tools.MySQManager;
import com.sxm.yiwei.tools.MySqHelper;
import com.sxm.yiwei.tools.REXTools;
import com.sxm.yiwei.tools.YiViMapActivity;
import com.sxm.yiwei.utils.BookStateChangeUtils;
import com.sxm.yiwei.utils.InternetAddressUtis;
import com.sxm.yiwei.views.MyGridView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.DecimalFormat;
import java.util.List;

public class BookContentHelpActivity extends BaseActivity implements View.OnClickListener{


    private ImageView helpBack;
    private TextView helpamount;
    private LinearLayout helpaddnumber;
    private LinearLayout helpcase;
    private TextView helpaddamount;
    private TextView helpcaseamount;
    private TextView helpalldistance;
    private TextView helpsendman;
    private TextView helpsendadd;
    private ImageView helpjiajia;
    private ImageView helpjiaji;
    private ImageView xiaofei;
    private LinearLayout helppics;
    private TextView helpcode;
    private TextView helpintro;
    private MyGridView helpgridimgs;
    private TextView helpstate;
    private TextView helpsendtime;
    private TextView helpservicetime;
    private TextView helpyivier;
    private Button helpweiyixai;
    private Button helpcall;
    private Button helpmap;
    private Button helpappeal;
    private Button helpselect;
    private Button helpcancel;
    private Button helpcrybook;
    private Button helpover;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String latitude;
    private String longitude;
    private String id;
    private int type;
    private String TAG ="help";
    private String consignorLat;
    private String consignorLong;
    private String consignorTel;
    private String orderId;
    private String courierMobile;
    private float distan;
    private Handler h = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    Bundle data = msg.getData();
                    String dis = data.getString("dis");
                    Double ddis = Double.valueOf(dis);
                    DecimalFormat df   = new DecimalFormat("######0.00");
                    double v = ddis / 1000;
                    helpalldistance.setText(df.format(v)+"公里");

                    break;
            }
        }
    };
    private Button helppick;
    private ImageView backmain;
    private String uid;

    /**
     * 订单详情 跑腿劳务
     * @param savedInstanceState
     *
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_content_help);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "");
        userId = sharedPreferences.getString("userId", "");
        token = sharedPreferences.getString("token", "");
        latitude = sharedPreferences.getString("latitude", "");
        longitude = sharedPreferences.getString("longitude", "");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        uid = intent.getStringExtra("uid");
        type = intent.getIntExtra("TYPE", 1);
        initHelpView();
        toViewButton(type);
        getBookContent();
    }
    // 底部按钮显示
    private void toViewButton(int t) {
        switch (t){
            case 2001:// 发单人-待抢单
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.GONE);
                helpcancel.setVisibility(View.VISIBLE);
                helpcall.setVisibility(View.GONE);
                helpweiyixai.setVisibility(View.GONE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.GONE);
                helpover.setVisibility(View.GONE);
                break;
            case 2002:// 待选定
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.GONE);
                helpcancel.setVisibility(View.VISIBLE);
                helpcall.setVisibility(View.GONE);
                helpweiyixai.setVisibility(View.GONE);
                helpselect.setVisibility(View.VISIBLE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.GONE);
                helpover.setVisibility(View.GONE);
                break;
            case 2003:// 待取货
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.VISIBLE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.VISIBLE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.GONE);
                break;
            case 2004:
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.VISIBLE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.VISIBLE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.GONE);
                break;
            case 2005:
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.VISIBLE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.VISIBLE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.VISIBLE);
                break;
            case 2006:
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.GONE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.GONE);
                helpweiyixai.setVisibility(View.GONE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.GONE);
                break;
            case 1001: // 一喂员 待选定
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.GONE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.GONE);
                helpweiyixai.setVisibility(View.GONE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.GONE);
                helpover.setVisibility(View.GONE);
                break;
            case 1002: // 待取货
                helppick.setVisibility(View.VISIBLE);
                helpmap.setVisibility(View.VISIBLE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.VISIBLE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.GONE);
                break;
            case 1003:// 配送中
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.VISIBLE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.VISIBLE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.VISIBLE);
                break;
            case 1004: // 待完结
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.VISIBLE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.VISIBLE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.GONE);
                break;
            case 1005: // 已完结
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.GONE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.GONE);
                helpweiyixai.setVisibility(View.GONE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.GONE);
                helpappeal.setVisibility(View.VISIBLE);
                helpover.setVisibility(View.GONE);
                break;
            case 3000:
                helppick.setVisibility(View.GONE);
                helpmap.setVisibility(View.GONE);
                helpcancel.setVisibility(View.GONE);
                helpcall.setVisibility(View.GONE);
                helpweiyixai.setVisibility(View.VISIBLE);
                helpselect.setVisibility(View.GONE);
                helpcrybook.setVisibility(View.VISIBLE);
                helpappeal.setVisibility(View.GONE);
                helpover.setVisibility(View.GONE);
                break;

        }
    }
    private void initHelpView() {
        helpBack = (ImageView) findViewById(R.id.content_helpback);
        helpamount = (TextView) findViewById(R.id.contenthelp_amount);
        helpaddnumber = (LinearLayout) findViewById(R.id.contenthelp_addnumber);
        helpcase = (LinearLayout) findViewById(R.id.contenthelp_case);
        helpaddamount = (TextView) findViewById(R.id.contenthelp_addamount);
        helpcaseamount = (TextView) findViewById(R.id.contenthelp_caseamount);
        helpalldistance = (TextView) findViewById(R.id.contenthelp_alldistance);
        helpsendman = (TextView) findViewById(R.id.contenthelp_sendman);
        helpsendadd = (TextView) findViewById(R.id.contenthelp_sendadd);
        helpjiajia = (ImageView) findViewById(R.id.contenthelp_jiajia);
        helpjiaji = (ImageView) findViewById(R.id.contenthelp_jiaji);
        xiaofei = (ImageView) findViewById(R.id.contenthelp_xiaofei);
        helppics = (LinearLayout) findViewById(R.id.contenthelp_intropics);
        helpcode = (TextView) findViewById(R.id.contenthelp_bookcode);
        helpintro = (TextView) findViewById(R.id.contenthelp_intro);
        helpgridimgs = (MyGridView) findViewById(R.id.contenthelp_introimgs);
        helpstate = (TextView) findViewById(R.id.contenthelp_bookstate);
        helpsendtime = (TextView) findViewById(R.id.contenthelp_sendtime);
        helpservicetime = (TextView) findViewById(R.id.contenthelp_servicetime);
        helpyivier = (TextView) findViewById(R.id.contenthelp_yivier);

        helpweiyixai = (Button) findViewById(R.id.contenthelp_weiyixia);
        helpcall = (Button) findViewById(R.id.contenthelp_call);
        helpmap = (Button) findViewById(R.id.contenthelp_map);
        helpappeal = (Button) findViewById(R.id.contenthelp_appeal);
        helpselect = (Button) findViewById(R.id.contenthelp_select);
        helpcancel = (Button) findViewById(R.id.contenthelp_cancel);
        helpcrybook = (Button) findViewById(R.id.contenthelp_crybook);
        helpover = (Button) findViewById(R.id.contenthelp_over);
        helppick = (Button) findViewById(R.id.contenthelp_pickup);
        backmain = (ImageView) findViewById(R.id.bookhelp_backmain);

        // 点击监听
        backmain.setOnClickListener(this);
        helpBack.setOnClickListener(this);
        helpweiyixai.setOnClickListener(this);
        helpcall.setOnClickListener(this);
        helpmap.setOnClickListener(this);
        helpappeal.setOnClickListener(this);
        helpselect.setOnClickListener(this);
        helpcrybook.setOnClickListener(this);
        helpcancel.setOnClickListener(this);
        helpover.setOnClickListener(this);
        helppick.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bookhelp_backmain:

                startActivity(new Intent(BookContentHelpActivity.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.content_helpback:
                finish();
                break;
            case R.id.contenthelp_weiyixia:
                Toast.makeText(this, "功能建设中，敬请期待", Toast.LENGTH_SHORT).show();
                break;
            case R.id.contenthelp_map:
                Intent i = new Intent(BookContentHelpActivity.this,YiViMapActivity.class);
                i.putExtra("mEndLatitude",Double.valueOf(latitude));
                i.putExtra("mEndLongitude",Double.valueOf(longitude));
                i.putExtra("mStartLatitude",Double.valueOf(consignorLat));
                i.putExtra("mStartLongitude",Double.valueOf(consignorLong));
                startActivity(i);
                break;
            case R.id.contenthelp_appeal:
                Intent appealintent = new Intent(BookContentHelpActivity.this,BookAppealActivity.class);
                appealintent.putExtra("id",orderId);
                startActivity(appealintent);
                break;
            case R.id.contenthelp_call:
                String phoneno = null;
                if (type>1000&type<2000){
                    phoneno = consignorTel;
                }else {
                    phoneno = courierMobile;
                }
                CallPhone.callPhone(BookContentHelpActivity.this,phoneno);
                break;
            case R.id.contenthelp_select:
                Intent intent1 = new Intent(BookContentHelpActivity.this,YiViListActivity.class);
                intent1.putExtra("id",orderId);
                startActivity(intent1);
                break;
            case R.id.contenthelp_over:
                if (type>1000&type<2000){
                    Intent i1 = new Intent(BookContentHelpActivity.this,PickUpActivity.class);
                    i1.putExtra("id",orderId);
                    i1.putExtra("from","2");
                    startActivity(i1);
                }else {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(BookContentHelpActivity.this);
                    View view = LayoutInflater.from(BookContentHelpActivity.this).inflate(R.layout.cancel_servicedialog, null);
                    Button cancelbt = (Button) view.findViewById(R.id.cancel_cancelbt);
                    Button surebt = (Button) view.findViewById(R.id.cancel_surebt);
                    TextView t1 = (TextView) view.findViewById(R.id.cancel_t1);
                    TextView t2 = (TextView) view.findViewById(R.id.cancel_t2);
                    builder.setView(view);
                    t2.setText("您确定要完结此单吗？");
                    surebt.setText("确定");
                    final AlertDialog overDialog = builder.create();
                    cancelbt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            overDialog.dismiss();
                        }
                    });
                    surebt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BookStateChangeUtils bookStateChangeUtils = new BookStateChangeUtils();
                            bookStateChangeUtils.cancelBook(orderId);

                            overDialog.dismiss();

                        }
                    });
                    overDialog.show();
                }

                break;
            case R.id.contenthelp_crybook:
                if (uid.equals(userId)){
                    Toast.makeText(this, "这是您自己的订单，自己不可以抢哦", Toast.LENGTH_SHORT).show();
                }else {
                    cryBook(id);
                }
                break;
            case R.id.contenthelp_cancel:
                BookStateChangeUtils bookStateChangeUtils = new BookStateChangeUtils();
                bookStateChangeUtils.cancelBook(orderId);
               // cancelBook(orderId);
                break;
            case R.id.contenthelp_pickup:
                Intent i1 = new Intent(BookContentHelpActivity.this,PickUpActivity.class);
                i1.putExtra("id",orderId);
                i1.putExtra("from","1");
                startActivity(i1);
                break;
        }
    }
    // 订单信息
    private void getBookContent() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.BOOKCONTENT);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        params.addParameter("params", j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(data);
                String consignorAddr = obj1.getString("consignorAddr");// 发货地
                String appointTime = obj1.getString("appointTime");// 预约时间
                String endTime = obj1.getString("endTime");// 结束时间
                String consignorHous = obj1.getString("consignorHous");// 结束时间
                // 经度
                consignorLat = obj1.getString("consignorLat");
                // 纬度
                consignorLong = obj1.getString("consignorLong");
                String consignorName = obj1.getString("consignorName"); // 发货人姓名
                // 发货人电话
                consignorTel = obj1.getString("consignorTel");
                String intro = obj1.getString("intro"); // 订单简介
                final JSONArray introImage1 = obj1.getJSONArray("introImage");//简介图片
                String makeupCount = obj1.getString("makeupCount"); // 加价次数
                String orderCode = obj1.getString("orderCode"); // 订单号
                // 订单id
                orderId = obj1.getString("orderId");
                String publishTime = obj1.getString("publishTime"); // 发单时间
                String sendStatus = obj1.getString("sendStatus"); // 订单状态
                String totalAmount = obj1.getString("totalAmount"); // 订单价格
                String type1 = obj1.getString("type"); // 订单类型

                // 一喂员电话
                courierMobile = obj1.getString("courierMobile");
                String courierNickname = obj1.getString("courierNickname"); // 一喂员
                // 设置数据
                helpamount.setText(totalAmount);

                helpintro.setText(intro);
                helpcode.setText(orderCode);
                if (type==3000){
                    helpsendman.setText(consignorName +" "+ REXTools.hidePhone(consignorTel));
                }else {
                    helpsendman.setText(consignorName +" "+consignorTel);
                }

                if (consignorHous==null){
                    if (type==3000){
                        helpsendadd.setText(REXTools.hideAddress(consignorAddr));
                    }else {
                        helpsendadd.setText(consignorAddr);
                    }

                }else {
                    if (type==3000){
                        helpsendadd.setText(REXTools.hideAddress(consignorAddr)+"****");
                    }else {
                        helpsendadd.setText(consignorAddr+consignorHous);
                    }

                }

                helpsendtime.setText(CastTime.CastTime(Long.valueOf(publishTime)));
                if (appointTime!=null){
                    helpservicetime.setText(CastTime.startTime(Long.valueOf(appointTime)));
                }else {
                    helpservicetime.setText("随时");
                }

                if (courierNickname!=null){// 显示一喂员
                    helpyivier.setText(courierNickname+" "+ courierMobile);
                }else {
                    helpyivier.setText(" ");
                }
                if (endTime!=null){
                    boolean b = CastTime.bookEndTime(Long.valueOf(endTime));
                    if (b){
                        helpappeal.setVisibility(View.GONE);
                    }else {

                    }
                }else {

                }

                if (makeupCount.equals("0")){ // 加价次数为 0

                }else { // 不为 0
                    helpaddnumber.setVisibility(View.VISIBLE);
                    helpaddamount.setText(makeupCount);
                    helpjiajia.setVisibility(View.VISIBLE);
                }

                if (sendStatus.equals("1")) {
                    helpstate.setText("待抢单");
                } else if (sendStatus.equals("2")) {
                    helpstate.setText("待选定");
                } else if (sendStatus.equals("3")) {
                    helpstate.setText("待服务");
                } else if (sendStatus.equals("4")) {
                    helpstate.setText("服务中");
                } else if (sendStatus.equals("5")) {
                    helpstate.setText("待完结");
                } else if (sendStatus.equals("6")) {
                    helpstate.setText("已完结");
                } else if (sendStatus.equals("0")) {
                    helpstate.setText("未支付订单");
                } else if (sendStatus.equals("-1")) {
                    helpstate.setText("已投诉");
                } else if (sendStatus.equals("-2")) {
                    helpstate.setText("订单已取消");
                }

                // 测距
                LatLonPoint sl = new LatLonPoint(Double.valueOf(latitude),Double.valueOf(longitude));
                LatLonPoint el = new LatLonPoint(Double.valueOf(consignorLat),Double.valueOf(consignorLong));
                getDispatchingstart(sl,el);



                if (introImage1.size()==0){

                }else {
                    helppics.setVisibility(View.VISIBLE);
                    FWContentImgAdapter imgAdapter = new FWContentImgAdapter(BookContentHelpActivity.this,introImage1);
                    helpgridimgs.setAdapter(imgAdapter);
                    int itemWidth = (int)(125 * helpgridimgs.getResources().getDisplayMetrics().density);
                    int itemSize = introImage1.size();
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemSize * itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
                    helpgridimgs.setLayoutParams(params);
                    helpgridimgs.setNumColumns(itemSize);
                    helpgridimgs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent in = new Intent(BookContentHelpActivity.this, ImgSwitcherActivity.class);
                            in.putExtra("position",position);
                            in.putExtra("imgs",introImage1);
                            startActivity(in);
                        }
                    });

                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    private void getDispatchingstart(LatLonPoint s , LatLonPoint e) {
        RouteSearch search = new RouteSearch(BookContentHelpActivity.this);
        RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(s,e);
        RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo,RouteSearch.DrivingShortDistance,null,null,"");
        search.calculateDriveRouteAsyn(query);
        search.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
            @Override
            public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {

            }

            @Override
            public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int i) {
                // 拿结果
                List<DrivePath> paths = driveRouteResult.getPaths();
                for (int i1 = 0; i1 < paths.size(); i1++) {
                    distan = paths.get(i1).getDistance();

                    Message m = new Message();
                    m.what=1;
                    Bundle b = new Bundle();
                    b.putString("dis", distan +"");
                    m.setData(b);
                    h.sendMessage(m);

                }
            }

            @Override
            public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {

            }

            @Override
            public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

            }
        });

    }

    public void cryBook(final String id){
        // 提示抢单
        AlertDialog.Builder b = new AlertDialog.Builder(BookContentHelpActivity.this);
        View view = LayoutInflater.from(BookContentHelpActivity.this).inflate(R.layout.crybook_dialogitem, null);
        b.setView(view);
        Button no = (Button) view.findViewById(R.id.dialog_no);
        Button yes = (Button) view.findViewById(R.id.dialog_yes);
        final AlertDialog dialog = b.create();
        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {

                // 发送请求 提示成功
                RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.CRYBOOK);
                params.addHeader("Content-Type", "application/json");
                params.addHeader("token", token);
                params.addHeader("appId", appId);
                params.addHeader("userId", userId);
                JSONObject object = new JSONObject();
                object.put("orderId", id);
                params.setBodyContent(object.toJSONString());
                x.http().post(params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        AlertDialog.Builder bu = new AlertDialog.Builder(BookContentHelpActivity.this, R.style.NoBackGroundDialog);
                        View view1 = LayoutInflater.from(BookContentHelpActivity.this).inflate(R.layout.crybook_okdialogitem, null);
                        bu.setView(view1);
                        Button okbt = (Button) view1.findViewById(R.id.dialog_okbt);
                        TextView message = (TextView) view1.findViewById(R.id.crybook_message);
                        final AlertDialog dialog1 = bu.create();
                        JSONObject obj1 = JSON.parseObject(result);
                        String status = obj1.getString("status");
                        if (status.equals("0")) {
                            String failure = obj1.getString("failure");
                            if (failure.equals("1028")) {
                                message.setText("您来晚了 订单已经被抢走了");
                            }else if(failure.equals("1011")){
                                message.setText("您提交的一喂员认证正在审核中，请耐心等待");
                            } else if (failure.equals("1029")) {
                                message.setText("为保证物品的安全,请申请一喂员,级别达到小工以上,才可以抢单");
                            } else if (failure.equals("1027")) {
                                message.setText("为保证物品的安全,请申请一喂员,级别达到小工以上,才可以抢单");
                            } else if (failure.equals("1030")) {
                                message.setText("此订单已您已经抢过了，再看看别的订单吧");
                            } else if (failure.equals("1032")) {
                                message.setText("订单已被对方取消，去看看别的订单吧");
                            } else if (failure.equals("1021")) {
                                message.setText("为保证物品的安全，级别达到小工以上才可以抢单 ");
                            } else if (failure.equals("10210")) {
                                message.setText("此单保价高于您的可接单额度(200),为保证物品的安全,需要达到小工级别以上才可以抢单 ");
                            } else if (failure.equals("10211")) {
                                message.setText("此单保价高于您的可接单额度(500),为保证物品的安全,需要达到贫农级别以上才可以抢单");
                            } else if (failure.equals("10212")) {
                                message.setText("此单保价高于您的可接单额度(1000),为保证物品的安全,需要达到富农级别以上才可以抢单");
                            } else if (failure.equals("10213")) {
                                message.setText("此单保价高于您的可接单额度(5000),为保证物品的安全,需要达到地主级别以上才可以抢单");
                            } else if (failure.equals("10214")) {
                                message.setText("此单保价高于您的可接单额度(1w),为保证物品的安全,需要达到知县级别以上才可以抢单");
                            } else if (failure.equals("10215")) {
                                message.setText("此单保价高于您的可接单额度(2w),为保证物品的安全,需要达到知府级别以上才可以抢单");
                            } else if (failure.equals("10216")) {
                                message.setText("此单保价高于您的可接单额度(5w),为保证物品的安全,需要达到总督级别以上才可以抢单");
                            } else if (failure.equals("10217")) {
                                message.setText("此单保价高于您的可接单额度(10w),为保证物品的安全,需要达到王爷级别以上才可以抢单");
                            } else {
                            }
                            okbt.setText("确定");
                            okbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog1.dismiss();
                                }
                            });
                        } else {
                            String data = obj1.getString("data");
                            JSONObject obj2 = JSON.parseObject(data);
                            String grabStatus = obj2.getString("grabStatus");

                            if (grabStatus.equals("0")) {
                                okbt.setText("确定");

                                message.setText("抢单成功，请等待选定吧");
                                okbt.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(BookContentHelpActivity.this, OrderSelectActivity.class);
                                        startActivity(intent);
                                        dialog1.dismiss();
                                    }
                                });
                            } else {
                                okbt.setText("去取货");
                                message.setText("抢单成功，快上门取货吧");
                                okbt.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(BookContentHelpActivity.this, OrderPickupActivity.class);

                                        startActivity(intent);
                                        dialog1.dismiss();
                                    }
                                });
                            }
                        }
                        dialog1.show();
                        dialog.dismiss();
                        MySqHelper helper = MySQManager.getInstance(BookContentHelpActivity.this);
                        SQLiteDatabase db = helper.getWritableDatabase();
                        String where = "oid = ?";
                        db.delete("yivic", where, new String[]{id});
                        db.close();

                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        ErrType.errType(BookContentHelpActivity.this, ex);
                    }

                    @Override
                    public void onCancelled(CancelledException cex) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });

            }
        });
        dialog.show();
    }

}
